---
title: "Main Results (IV)"
author: "Andrei Munteanu"
date: "11/01/2022"
output:
  html_document:
    fig_width: 8 
    fig_height: 8 
  # html_document:
    # toc: true 
    # toc_float: true
---
<style type="text/css">
.main-container {
  max-width: 1800px;
  margin-left: auto;
  margin-right: auto;
}
</style>

```{r options, include=FALSE, echo=FALSE, cache=FALSE}
knitr::opts_knit$set(root.dir=getwd())
knitr::opts_chunk$set(cache.lazy=FALSE,cache=TRUE,warning=FALSE,echo=TRUE,comment='',prompt=T)
options(modelsummary_format_numeric_latex='plain')
knitr::opts_template$set(
  regular=list(include=TRUE,echo=TRUE,cache=FALSE,warning=FALSE,message=FALSE),
  regular_cache=list(include=TRUE,echo=TRUE,cache=TRUE,warning=FALSE,message=FALSE),
  invisible=list(include=FALSE,echo=FALSE,cache=FALSE,warning=FALSE,results=FALSE,message=FALSE),
  muted=list(include=TRUE,echo=TRUE,cache=FALSE,warning=FALSE,results=FALSE,message=FALSE),
  latex=list(include=TRUE,echo=TRUE,cache=FALSE))

perc.rank <- function(x) {
      y<-rank(x)/length(x)
      return(y)}
```

```{r, opts.label='regular'}

# perc.rank.zero <- function(x) {
#       
#        y<-ifelse(x==0,0,rank(x)/length(x))
#       return(y)}

#get town/school stats including dropouts/non-matched students
data_regression<-data_regression %>%
  mutate(pass=ifelse(grepl("Reu|REU",rezultat,perl=T),1,0)) %>%
  mutate(absent=ifelse(grepl("Nep|NEP",rezultat,perl=T),1,0)) %>%
  mutate(match=ifelse(!is.na(rezultat) & !is.na(media_la_admitere),1,0)) 

data_regression2<-data_regression %>%
  filter(an %in% c(2008:2019)) %>%
  group_by(judet_adm,liceu_repartizat) %>%
  mutate(school_mean=mean(entrance_perc,na.rm=T)) %>%
  group_by(judet_adm,liceu_repartizat,specializare_adm) %>%
  mutate(class_mean=mean(entrance_perc,na.rm=T)) 

data_regression2$entrance_perc_town<-NA
data_regression2[!is.na(data_regression2$media_la_admitere),]<-data_regression2[!is.na(data_regression2$media_la_admitere),] %>%
      group_by(an_adm,Cod_SIRUTA2_hs_adm) %>%
      mutate(entrance_perc_town=perc.rank(media_la_admitere)) %>%
      ungroup
data_regression2<-data_regression2 %>% mutate(dec_town=cut(entrance_perc_town, breaks = c(-Inf, 0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9, Inf), 
                                                       labels = c('1','2','3','4','5','6','7','8','9','10'), right = FALSE))
    
    

#add missing values for students with missing graduation exam scores
data_regression2<-data_regression2 %>%
  group_by(Cod_SIRUTA2_hs_adm) %>%
  mutate(Wages_hs_bac=ifelse(is.na(Wages_hs_bac),mean(Wages_hs_bac,na.rm=T),Wages_hs_bac),
         drop_hs_hs_adm=ifelse(is.na(drop_hs_hs_bac),mean(drop_hs_hs_bac,na.rm=T),drop_hs_hs_bac),
         Unemployment_hs_bac=ifelse(is.na(Unemployment_hs_bac),mean(Unemployment_hs_bac,na.rm=T),Unemployment_hs_bac),
         n_students_town_yr=ifelse(is.na(n_students_town_yr),mean(n_students_town_yr,na.rm=T),n_students_town_yr),
         n_hs_town=ifelse(is.na(n_hs_town),mean(n_hs_town,na.rm=T),n_hs_town)) %>%
  group_by(judet_adm) %>%
          mutate(Unemployment_hs_bac=ifelse(is.na(Unemployment_hs_bac),mean(Unemployment_hs_bac,na.rm=T),Unemployment_hs_bac),
          drop_hs_hs_bac=ifelse(is.na(drop_hs_hs_bac),mean(drop_hs_hs_bac,na.rm=T),drop_hs_hs_bac),
          Wages_hs_bac=ifelse(is.na(Wages_hs_bac),mean(Wages_hs_bac,na.rm=T),Wages_hs_bac)) %>%
  ungroup


data_regression2<-as.data.frame(data_regression2)
data_regression2$n_hs_town_group<-data_regression2$n_hs_town
data_regression2$n_hs_town_group[data_regression2$n_hs_town>=4 & data_regression2$n_hs_town<=15]<-"4-15"
data_regression2$n_hs_town_group[data_regression2$n_hs_town>15]<-"16+"
data_regression2$n_hs_town_group<-with(data_regression2, reorder(n_hs_town_group, n_hs_town))


data_regression2<-data_regression2 %>% 
  filter(!is.na(media_la_admitere))

data_regression2<-data_regression2 %>%
 mutate(dec_town=relevel(dec_town,ref="6"))
```

## {.tabset}
### Match
```{r, opts.label='regular'}
model_match<-feols(I(grad_perc_0!=0)~entrance_perc+school_mean+n_hs_town_group+dec_town*n_students_town_yr+Unemployment_hs_adm*dec_town+Wages_hs_adm*dec_town+drop_hs_hs_adm*dec_town|
                         as.factor(an)+specializare_adm+scoala_de_provenienta+Cod_SIRUTA2_hs_adm,
                        cluster=~judet_adm,
                       data=data_regression2)
summary(model_match)
```


### Change School
Run regression:
```{r, opts.label='regular'}
model_change<-feols(I(school_change==TRUE)~entrance_perc+dec_town+n_hs_town_group+dec_town*n_students_town_yr+Unemployment_hs_bac*dec_town+Wages_hs_bac*dec_town+drop_hs_hs_bac*dec_town|
        as.factor(an)+specializare_bac2+scoala_de_provenienta+town|
        school_mean~dec_town*n_hs_town_group,
      cluster=~judet_bac,
      data=data_regression2)
summary(model_change)
```

### Pass bac
Run regression:
```{r, opts.label='regular'}
model_school_pass<-feols(I(pass*100)~entrance_perc+dec_town+n_hs_town_group+dec_town*n_students_town_yr+Unemployment_hs_bac*dec_town+Wages_hs_bac*dec_town+drop_hs_hs_bac*dec_town|
        as.factor(an)+specializare_bac2+scoala_de_provenienta+town|
        school_mean~dec_town*n_hs_town_group,
      cluster=~judet_bac,
      data=data_regression2)
summary(model_school_pass)
```

### Latex 2nd
```{r, opts.label='latex'}
f_big<-function(x) format(x, big.mark=",", scientific=FALSE, nsmall=1,digits=1)
f <- function(x) format(round(x/1000000,1) , nsmall = 1)
options(modelsummary_format_numeric_latex = "plain")
print(modelsummary(list(model_match,model_change,model_school_pass), 
              statistic = "std.error",
             estimate="{estimate}{stars}",
                    stars=c('^{*}'=0.1,'^{**}'=0.05,'^{***}'=0.01),
               gof_map=list(list("raw" = "nobs", "clean" = "N", "fmt" = f),
                          list("raw" = "r.squared", "clean" = "R$^2$",fmt="%.2f")),
              metrics="R2",
              output="latex",
                escape=F))






r2(model_match)[2]
r2(model_change)[2]
r2(model_school_pass)[2]
```





